package com.ajax.servlet; /**
 * @author wzm
 * @create 2023-03-23 10:42
 */

import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet("/insertStu")
public class InsertStudentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        response.setContentType("text/json;charset=utf-8");

        //通用的获取参数有的方式在使用jsp时没有影响 JSON数据提交post参数的只能使用post特有的获取方式
//        String name = request.getParameter("name");
//        System.out.println("name = " + name);

        //获取请求参数中的请求体信息
        BufferedReader reader = request.getReader();
        String s = reader.readLine();//name=xx&xx=xx

        //将获取的字符串转为java对象
        Student student = JSON.parseObject(s, Student.class);

        //模拟调用service新增的方法
        //boolean flag = studentService.save(student);
        System.out.println(student);

        boolean flag = true;//新增成功
        String result = "error";

        //判断执行结果并返回
        if(flag){
            result = "success";
        }
        response.getWriter().write(result);
    }
}
